package com.example.database.demo.config;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.context.annotation.Configuration;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 梅芙雨Vanessa
 */
@Configuration
public class SnowflakeConfig {

    private static final long WORKER_ID = 1;
    private static final long DATA_CENTER_ID = 1;

    private static final Snowflake SNOWFLAKE = IdUtil.getSnowflake(WORKER_ID, DATA_CENTER_ID);
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss");
    private static final Object LOCK = new Object();

    public static String generateUniqueId() {
        // 获取序列号
        synchronized (LOCK) {
            return DATE_FORMAT.format(new Date()) + SNOWFLAKE.nextId();
        }
    }
}
